package com.inifitness.tenant.core.system.service.impl;


import java.util.List;
import java.util.Set;

import jakarta.annotation.PostConstruct;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inifitness.common.constants.SecurityConstants;
import com.inifitness.tenant.core.system.mapper.BizRoleMenuMapper;
import com.inifitness.tenant.core.system.model.bo.BizRolePermsBO;
import com.inifitness.tenant.core.system.model.entity.BizRoleMenu;
import com.inifitness.tenant.core.system.service.BizRoleMenuService;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 角色菜单业务实现
 *
 * @author sunjinfeng
 * @since 2024/04/18 11:00:00
 */
@Service
@RequiredArgsConstructor
public class BizRoleMenuServiceImpl extends ServiceImpl<BizRoleMenuMapper, BizRoleMenu> implements
    BizRoleMenuService {

  private final RedisTemplate<String, Object> redisTemplate;

  /**
   * 初始化权限缓存
   */
  @PostConstruct
  public void initRolePermsCache() {
    refreshRolePermsCache();
  }

  /**
   * 刷新权限缓存
   */
  @Override
  public void refreshRolePermsCache() {
    // 清理权限缓存
    redisTemplate.opsForHash().delete(SecurityConstants.ROLE_PERMS_PREFIX, "*");

    List<BizRolePermsBO> list = this.baseMapper.getRolePermsList(null);
    if (CollUtil.isNotEmpty(list)) {
      list.forEach(item -> {
        String roleCode = item.getRoleCode();
        Set<String> perms = item.getPerms();
        redisTemplate.opsForHash().put(SecurityConstants.ROLE_PERMS_PREFIX, roleCode, perms);
      });
    }
  }

  /**
   * 刷新权限缓存
   */
  @Override
  public void refreshRolePermsCache(String roleCode) {
    // 清理权限缓存
    redisTemplate.opsForHash().delete(SecurityConstants.ROLE_PERMS_PREFIX, roleCode);

    List<BizRolePermsBO> list = this.baseMapper.getRolePermsList(roleCode);
    if (CollUtil.isNotEmpty(list)) {
      BizRolePermsBO rolePerms = list.get(0);
      if (rolePerms == null) {
        return;
      }

      Set<String> perms = rolePerms.getPerms();
      redisTemplate.opsForHash().put(SecurityConstants.ROLE_PERMS_PREFIX, roleCode, perms);
    }
  }

  /**
   * 刷新权限缓存 (角色编码变更时调用)
   */
  @Override
  public void refreshRolePermsCache(String oldRoleCode, String newRoleCode) {
    // 清理旧角色权限缓存
    redisTemplate.opsForHash().delete(SecurityConstants.ROLE_PERMS_PREFIX, oldRoleCode);

    // 添加新角色权限缓存
    List<BizRolePermsBO> list = this.baseMapper.getRolePermsList(newRoleCode);
    if (CollUtil.isNotEmpty(list)) {
      BizRolePermsBO rolePerms = list.get(0);
      if (rolePerms == null) {
        return;
      }

      Set<String> perms = rolePerms.getPerms();
      redisTemplate.opsForHash().put(SecurityConstants.ROLE_PERMS_PREFIX, newRoleCode, perms);
    }
  }


  /**
   * 获取角色拥有的菜单ID集合
   *
   * @param roleId 角色ID
   * @return 菜单ID集合
   */
  @Override
  public List<Long> listMenuIdsByRoleId(Long roleId) {
    List<Long> menuIds = this.baseMapper.listMenuIdsByRoleId(roleId);
    return menuIds;
  }

}
